﻿using Dapper;
using Student.Common;

using Student.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Student.DAL
{
    /// <summary>
	/// UserToken的数据操作类
	/// </summary>
	public partial class UserTokenDAO
    {
        public UserToken? GetInfoByToken(string token)
        {
            var sql = "SELECT * FROM UserToken WHERE Token = @Token";
            using var conn = ConnectionFactory.CreateConnection();
            var model = conn.QueryFirstOrDefault<UserToken>(sql, new { Token = token });
            return model;
        }

        public List<UserToken>? GetEffectiveList(int userDataID, int userClient, IDbTransaction? trans)
        {
            var sql = "SELECT * FROM UserToken WHERE UserDataID = @UserDataID AND UserClient = @UserClient AND ExpiredTime > GETDATE()";
            if (trans == null)
            {
                using var conn = ConnectionFactory.CreateConnection();
                var list = conn.Query<UserToken>(sql, new { UserDataID = userDataID, UserClient = userClient });
                return list.ToList();
            }
            var conn2 = trans.Connection;
            var list2 = conn2?.Query<UserToken>(sql, new { UserDataID = userDataID, UserClient = userClient }, transaction: trans);
            return list2?.ToList();
        }
    }
}